<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

	var $title   = '';
	var $content = '';
	var $date    = '';

	function __construct()
	{ 
		parent::__construct();
	}

	function checkuser($username,$password)
	{
		$this->load->database('default');
		$query = $this->db->get_where('tbl_admin',array('username' => $username,'password'=>$password,'status'=>1),1,0);
		if($query->num_rows()>0){
			foreach ($query->result() as $row)
			{
				$userkey =  $row->id;
			}
			return $userkey;
		} else {
			return 0;
		}
	}

	function checkexit($username)
	{
		$this->load->database('default');
		$query = $this->db->get_where('tbluser',array('uname' => $username),1,0);
		if($query->num_rows()>0){
			foreach ($query->result() as $row)
			{
				$userkey =  $row->ukey;
			}
			return $userkey;
		} else {
			return 0;
		}
	}

	function get_alluser()
	{
		$this->load->database();
		$query = $this->db->get('tbluser');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
		 
	}

	function deleteuser($id)
	{
		$this->load->database('default');
		$this->db->delete('tbluser', array('uid' => $id));
	}

	function insert_user($name,$pass,$active,$key,$avatar,$type)
	{
		$this->load->database('default');
		$data = array(
			'uname' => $name ,                
			'ucreatedate' =>  date("Y-m-d H:i:s"),
        	'upass' => $pass,
        	'uactive' => $active,
        	'ukey' =>  $key ,
        	'utype' =>  $type,
        	'avatar' => $avatar
		);
		$this->db->insert('tbluser', $data);
	}

	function update_entry()
	{
		$this->title   = $_POST['title'];
		$this->content = $_POST['content'];
		$this->date    = time();
		$this->db->update('tbluser', $this, array('id' => $_POST['id']));
	}

}